﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform.Dc
{
    /// <summary>
    /// 医保诊断字典
    /// </summary>
    public class HiDiagDictService : DcAppService
    {
        private readonly IRepository<HiDiagDict, Guid> _hiDiagListRepository;

        public HiDiagDictService(IRepository<HiDiagDict, Guid> hiDiagListRepository)
        {
            _hiDiagListRepository = hiDiagListRepository;
        }

        /// <summary>
        /// 获取医保诊断列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedResultDto<HiDiagDictDto>> GetListAsync(HiDiagDictInput input)
        {
            var iQuery = await _hiDiagListRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.Key), d => d.DiagName.Contains(input.Key));

            if (input.SkipCount == 0)
            {
                var itemsDto = ObjectMapper.Map<List<HiDiagDict>, List<HiDiagDictDto>>(iQuery.ToList());

                return new PagedResultDto<HiDiagDictDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.SkipCount, input.MaxResultCount);
                var itemsDto = ObjectMapper.Map<List<HiDiagDict>, List<HiDiagDictDto>>(items.ToList());

                return new PagedResultDto<HiDiagDictDto>(totalCount, itemsDto);
            }
        }
    }
}
